class ProductAttributeTemplatesController < ApplicationController
  # GET /product_attribute_templates
  # GET /product_attribute_templates.xml
  layout "admin",:only=>:index
    before_filter :admin?
  def index
    load_data
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @product_attribute_templates }
    end
  end

  # GET /product_attribute_templates/1
  # GET /product_attribute_templates/1.xml
  def show
    @product_attribute_template = ProductAttributeTemplate.find(params[:id])
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @product_attribute_template }
    end
  end

  # GET /product_attribute_templates/new
  # GET /product_attribute_templates/new.xml
  def new
    @product_attribute_template = ProductAttributeTemplate.new
    load_data
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @product_attribute_template }
    end
  end

  # GET /product_attribute_templates/1/edit
  def edit
    @product_attribute_template = ProductAttributeTemplate.find(params[:id])
    load_data
  end

  # POST /product_attribute_templates
  # POST /product_attribute_templates.xml
  def create
    @product_attribute_template = ProductAttributeTemplate.new(params[:product_attribute_template])

    respond_to do |format|
      if @product_attribute_template.save
        load_data
        flash[:notice] = 'ProductAttributeTemplate was successfully created.'
        format.html { redirect_to(@product_attribute_template) }
        #format.js {render :partial=>"list_templates",:object=>@product_attribute_templates}
        format.js do
          render :update do |page|
            page.replace_html  'display_template', :partial => 'list_templates', :object=>@product_attribute_templates
            page.replace_html  'operate',""
          end
        end
        format.xml  { render :xml => @product_attribute_template, :status => :created, :location => @product_attribute_template }
      else
        format.html { render :action => "new" }
        format.js { render :action => "new" }
        format.xml  { render :xml => @product_attribute_template.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /product_attribute_templates/1
  # PUT /product_attribute_templates/1.xml
  def update
    @product_attribute_template = ProductAttributeTemplate.find(params[:id])

    respond_to do |format|
      if @product_attribute_template.update_attributes(params[:product_attribute_template])
        load_data
        flash[:notice] = 'ProductAttributeTemplate was successfully updated.'
        format.html { redirect_to(@product_attribute_template) }
        format.js {render :partial=>"list_templates",:object=>@product_attribute_templates}
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.js {render :action => "edit" }
        format.xml  { render :xml => @product_attribute_template.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /product_attribute_templates/1
  # DELETE /product_attribute_templates/1.xml
  def destroy
    @product_attribute_template = ProductAttributeTemplate.find(params[:id])
    @product_attribute_template.destroy
    load_data
    respond_to do |format|
      format.html { redirect_to(product_attribute_templates_url) }
      format.js {render :partial=>"list_templates",:object=>@product_attribute_templates}
      format.xml  { head :ok }
    end
  end

  private
  def load_data
    @product_attribute_templates = ProductAttributeTemplate.find(:all,:order=>"ord asc,id asc")
    @lookuplists ||= Lookuplist.all
    @template_categories||=TemplateCategory.all
    @data_types||=DataType.all
  end
end
